package io.renren.modules.lucky.service.impl;

import cn.hutool.core.util.StrUtil;
import io.renren.common.utils.AESUtil;
import io.renren.common.utils.Result;
import io.renren.common.utils.UnionKeyUtil;
import io.renren.modules.lucky.dto.background.AddUserReq;
import io.renren.modules.lucky.dto.background.LuckyBillUpdateDto;
import io.renren.modules.lucky.entity.LuckyBillEntity;
import io.renren.modules.lucky.entity.LuckyUserConfigEntity;
import io.renren.modules.lucky.service.LuckyAdminService;
import io.renren.modules.lucky.service.LuckyBillService;
import io.renren.modules.lucky.service.LuckyUserConfigService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LuckyAdminServiceImpl implements LuckyAdminService {
    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(LuckyAdminServiceImpl.class);

    @Autowired
    private LuckyUserConfigService luckyUserConfigService;

    @Autowired
    private LuckyBillService luckyBillService;

    @Override
    public Result addUser(AddUserReq addUserReq) {
        LuckyUserConfigEntity userConfig = new LuckyUserConfigEntity();
        userConfig.setPlatCode(addUserReq.getPlatCode());
        userConfig.setPlatName(addUserReq.getPlatName());
        userConfig.setUid(addUserReq.getUid());
        userConfig.setForecastRevenueRatio(addUserReq.getForecastRevenueRatio());

        //AES
        userConfig.setAesKey(AESUtil.generateAESKey(128));
        userConfig.setUnionKey(UnionKeyUtil.generateRandomString(32));

        if (luckyUserConfigService.insert(userConfig)) {
            return new Result().ok("添加成功");
        } else {
            return new Result().error("添加失败");
        }
    }

    @Override
    public Result updateBill(LuckyBillUpdateDto updateDto) {
        LuckyBillEntity update = new LuckyBillEntity();
        update.setId(updateDto.getId());
        update.setStatus(updateDto.getStatus());

        if (StrUtil.isNotBlank(updateDto.getBillInfo())) {
            update.setBillInfo(updateDto.getBillInfo());
        }
        if (StrUtil.isNotBlank(updateDto.getAuditOpinion())) {
            update.setAuditOpinion(updateDto.getAuditOpinion());
        }
        boolean flag = luckyBillService.updateById(update);
        if (flag) {
            return new Result().ok("更新成功");
        }
        return new Result().error("更新失败");
    }
}
